package com.xinghou.platform.server.api.controller;

import com.xinghou.platform.entity.req.ListReq;
import com.xinghou.platform.entity.req.SubjectReplyReq;
import com.xinghou.platform.entity.req.SubjectReq;
import com.xinghou.platform.entity.resp.SubjectResp;
import com.xinghou.platform.model.main.Subject;
import com.xinghou.platform.model.main.SubjectReply;
import com.xinghou.platform.model.main.SubjectReplyPraise;
import com.xinghou.platform.server.api.bussiness.SubjectService;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by qingshengzheng on 16/6/23.
 */
@Controller
@SessionAttributes("token")
public class SubjectController extends  BaseControl {
    @Resource
    public SubjectService subjectService;

    /*@Resource
    private UserInfoBusinessService userService;*/

    /**
     * 话题列表
     * @param subjectReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getSubjects",method = {RequestMethod.POST})
    public String getSubjects( @RequestBody SubjectReq subjectReq,
                                HttpServletRequest request, HttpServletResponse response)
    {
        List<SubjectResp> subjects =null;
        try {
           subjects=subjectService.getSubjects(Long.parseLong(subjectReq.getFromno()), Integer.parseInt(subjectReq.pagesize),subjectReq.getUserid());
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }
       return response(subjects);
    }

    /**
     * 话题列表
     * @param listReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/subjectList",method = {RequestMethod.POST})
    public String subjectList( @RequestBody ListReq listReq,
                               HttpServletRequest request, HttpServletResponse response)
    {
        List<SubjectResp> subjects= null;
        try {
             subjects=subjectService.getSubjectList(listReq);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }
        return response(subjects);
    }


    /**
     * 话题回复列表
     * @param listReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/subjectReplyList",method = {RequestMethod.POST})
    public String subjectReplyList( @RequestBody ListReq listReq,
                               HttpServletRequest request, HttpServletResponse response)
    {
        List<SubjectReply> subjects = null;
        try {
            subjects=subjectService.subjectReplyList(listReq);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }

        return response(subjects);
    }

    /**
     * 发布话题
     * @param subjectReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/pubSubject", method = {RequestMethod.POST})
    public String pubSubject(@RequestBody SubjectReq subjectReq,
                             HttpServletRequest request, HttpServletResponse response) {
        Integer id =subjectService.pubSubject(subjectReq.toSubject(),subjectReq.joinnum);
        return response(id);
    }

    /**
     * 更新话题
     * @param subjectReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateSubject", method = {RequestMethod.POST})
    public String updateSubject( @RequestBody SubjectReq subjectReq,
                             HttpServletRequest request, HttpServletResponse response) {
        subjectService.updateSubject(subjectReq.toSubject(),subjectReq.getJoinnum());
        return response(null);
    }

    /**
     * 更新话题回复
     * @param subjectReplyReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateSubjectReply", method = {RequestMethod.POST})
    public String updateSubjectReply( @RequestBody SubjectReplyReq subjectReplyReq,
                                 HttpServletRequest request, HttpServletResponse response) {
        subjectService.updateSubjectReply(subjectReplyReq.toSubjectReply());
        return response(null);
    }

    /**
     *话题详情
     * @param subjectReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/subjectDetail", method = {RequestMethod.POST})
    public String subjectDetail( @RequestBody SubjectReq subjectReq,
                                HttpServletRequest request, HttpServletResponse response) {
        Long userid = subjectReq.getUserid()==null?Long.valueOf(0):Long.parseLong(subjectReq.getUserid());
        SubjectResp subject=subjectService.subjectDetail( userid ,Long.parseLong(subjectReq.getId()));
        subjectService.addSubjectJoinNum( userid , Long.parseLong(subjectReq.getId()));
        return response(subject);
    }
    /**
     * 删除话题
     * @param subjectReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delSubject", method = {RequestMethod.POST})
    public String delSubject( @RequestBody SubjectReq subjectReq,
                                HttpServletRequest request, HttpServletResponse response) {
        subjectService.delSubject(Long.parseLong(subjectReq.getId()),subjectReq.getOperator());
        return response(null);
    }

    /**
     * 回复话题
     * @param subjectReplyReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/subjectReply", method = {RequestMethod.POST})
    public String subjectReply( @RequestBody SubjectReplyReq subjectReplyReq,
                             HttpServletRequest request, HttpServletResponse response) {

       Long id = subjectService.subjectReply(subjectReplyReq.toSubjectReply(),subjectReplyReq.uasystem);
        return response(id);
    }

    /**
     * 删除话题回复
     * @param subjectReplyReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delSubjectReply", method = {RequestMethod.POST})
    public String delSubject(@RequestBody SubjectReplyReq subjectReplyReq,
                               HttpServletRequest request, HttpServletResponse response) {
        subjectService.delSubjectReply(Long.parseLong(subjectReplyReq.id),Long.parseLong(subjectReplyReq.subjectid));
        return response(null);
    }


    /**
     * 热门话题回复
     * @param subjectReplyReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/hotSubjectReplys", method = {RequestMethod.POST})
    public String hotSubjectReplys( @RequestBody SubjectReplyReq subjectReplyReq,
                               HttpServletRequest request, HttpServletResponse response) {
        String uid =subjectReplyReq.getUserid()==null?"":subjectReplyReq.getUserid();
       List<SubjectReply> list = subjectService.getHotReplys(Long.parseLong(subjectReplyReq.subjectid),uid);
        return response(list);
    }

    /**
     * 热门话题回复
     * @param subjectReplyReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/hotSubjectReplysList", method = {RequestMethod.POST})
    public String hotSubjectReplysList( @RequestBody SubjectReplyReq subjectReplyReq,
                                    HttpServletRequest request, HttpServletResponse response) {
        String uid =subjectReplyReq.getUserid()==null?"":subjectReplyReq.getUserid();
        List<SubjectReply> list = subjectService.hotSubjectReplysList(Long.parseLong(subjectReplyReq.subjectid), uid);
        return response(list);
    }




    /**
     * 最新话题
     * @param subjectReplyReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getLastSubject",method = {RequestMethod.POST,RequestMethod.GET})
    public String getLastSubject( @RequestBody SubjectReq subjectReplyReq,
                                HttpServletRequest request, HttpServletResponse response)
    {
        SubjectResp resp= null;
        try {
            resp= subjectService.getLastSubjectResp();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(DBExceptionEnums.SERVER_INTEERFACE_ERROR);
        }


        return response(resp);
    }

    @ResponseBody
    @RequestMapping(value="/getSubjectReplys",method = {RequestMethod.POST})
    public String subjectReplys(@RequestBody SubjectReplyReq subjectReplyReq,
                                HttpServletRequest request, HttpServletResponse response)
    {
        String uid =subjectReplyReq.getUserid()==null?"":subjectReplyReq.getUserid();
        List<SubjectReply> replys=subjectService.getReplys(Long.parseLong(subjectReplyReq.subjectid),subjectReplyReq.fromno,subjectReplyReq.pagesize,uid,subjectReplyReq.direction,subjectReplyReq.timestamp);
        return response(replys);

    }

    /**
     * 点赞
     * @param subjectReplyPraise
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/likeReply", method = {RequestMethod.POST})
    public String likeReply( @RequestBody SubjectReplyPraise subjectReplyPraise,
                                   HttpServletRequest request, HttpServletResponse response) {
        subjectService.likeReply(subjectReplyPraise);
        return response(null);
    }

    /**
     * 分享计数
     * @param subjectReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/subjectShareCount", method = {RequestMethod.POST})
    public String likeReply( @RequestBody SubjectReq subjectReq,
                             HttpServletRequest request, HttpServletResponse response) {
        subjectService.updateSubjectShareCount(subjectReq.getId());
        return response(null);
    }

    /**
     * 点击计数
     * @param subjectReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/subjectClickCount", method = {RequestMethod.POST})
    public String subjectClickCount( @RequestBody SubjectReq subjectReq,
                             HttpServletRequest request, HttpServletResponse response) {
        subjectService.updateSubjectClickCount(subjectReq.getId());
        return response(null);
    }

    /**
     * 点击计数
     * @param listReq
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getUserSubjectReply", method = {RequestMethod.POST})
    public String getUserSubjectReply( @RequestBody ListReq listReq,
                                     HttpServletRequest request, HttpServletResponse response) {
        List<SubjectReply> list = subjectService.getUserSubjectReply(listReq);
        return response(list);
    }






    @Scheduled(cron = "0 0 * * * ?")
    public void updateSubjectJoin() {
        List<Subject> list = subjectService.getLastThreeDaySubject();
        for(Subject subject :list) {
            int joinnum= 1000+ (int)(Math.random()*4000);
            subjectService.updateRandomJoinnum(subject.id,joinnum);
        }
    }
}
